'''数据处理层'''

import pymysql
host='localhost'
port=3311
user='root'
password='123456'
database='market'
charset='utf8'
def data_conn():   #创建连接函数
    db=pymysql.Connect(host=host,port=port,user=user,password=password,database=database,charset=charset)
    # print("数据库已连接")
    return db

def read_data(sql):
    db=data_conn()
    cursor=db.cursor()
    data=None
    try:
        cursor.execute(sql)
        data=cursor.fetchall()
        # print("数据已读取")
    except Exception as e:
        print(e)
    cursor.close()
    db.close()
    return data
def write_data(sql):   #写入数据
    db=data_conn()
    cursor=db.cursor(sql)
    rows=0
    try:
        rows=cursor.execute(sql)
        db.commit()   #上传数据
    except Exception as e:
        db.rollback()   #如果出错回滚数据
        print(e)
    cursor.close()
    db.close()
    return rows
if __name__ == '__main__':
    data_conn()
    # 查询
    sql=f"""select * from goods"""
    data=read_data(sql)
    for row in data:
        print(f'{row[1]}------{row[2]}-----{row[3]}-------{row[4]}')
    # 写入
    numbers=int(input("请输入您要购买的数量："))
    # sql = f"""insert into t1 values(null,{numbers},'666666')"""
    # rows = write_data(sql)
    # print(rows)

